import 'package:flutter_admin/api/odoo/dynamic_data_api.dart';
import 'package:flutter_admin/api/odoo/stock_move_line_odoo_api.dart';
import 'package:flutter_admin/generated/l10n.dart';
import 'package:flutter_admin/models/odoo/finish_in.dart';
import 'package:flutter_admin/models/odoo/stock_move_line.dart';
import 'package:flutter_admin/models/odoo/stock_quant.dart';
import 'package:flutter_admin/models/user_info.dart';
import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:flutter_admin/utils/store_util.dart';
import 'package:intl/intl.dart';
import 'package:quiver/strings.dart';

class StockQuantPintDialog extends StatefulWidget {
  StockQuantPintDialog();

  @override
  _StockQuantPintDialogState createState() => _StockQuantPintDialogState();
}

class _StockQuantPintDialogState extends State<StockQuantPintDialog> {
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();

  List<StockQuant> selectedList = [];
  //车间
  List<SelectOptionVO> workshopList = [];
  //库房
  List<SelectOptionVO> locationList = [];

  StockQuant stockQuant = StockQuant();

  UserInfo userInfo = StoreUtil.getCurrentUserInfo();

  var format= DateFormat('yyyy-MM-dd');

  @override
  void initState() {
    super.initState();
    _loadSelectVO();
  }

  _loadSelectVO() async {
    ResponseBodyApi res = await StockMoveLineOdooApi.getLocation(userInfo.userId);
    if (res.success!){
      for(var i = 0; i < res.data.length; i++){
        locationList.add(SelectOptionVO(value: res.data[i]["locaiton_id"].toString(),label: res.data[i]["locaiton_name"]));
      }
      if(res.data.length == 1){
        stockQuant.locationId = res.data[0]["location_id"].toString();
      }
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CrySelect(
            label: '库房',
            dataList: locationList,
            value: stockQuant.locationId,
            onSaved: (v) {
              stockQuant.locationId = v;
            },
          ),
          CryInput(
            label: '物料编码',
            value: stockQuant.productCode,
            onSaved: (v) {
              stockQuant.productCode = v;
            },
          ),
          CryInput(
            label: '物料名称',
            value: stockQuant.productName,
            onSaved: (v) {
              stockQuant.productName = v;
            },
          ),
        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: "确定",
          onPressed: () {
            FormState form = formKey.currentState!;
            form.save();
            Navigator.pop(context, stockQuant);
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text("查询"),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}
